#include "lcmusic.h"

static void lcmusic_play_event_callback(audio_player_cb_ctx_t *ctx)
{
    ESP_LOGI(TAG, "ctx->audio_event = %d", ctx->audio_event);

    lcmusic_app_data_t *lcmusic_app_data = (lcmusic_app_data_t *) ctx->user_ctx;

    switch (ctx->audio_event) {
    case AUDIO_PLAYER_CALLBACK_EVENT_IDLE: 
    {  // 播放完一首歌 进入这个case
        ESP_LOGI(TAG, "AUDIO_PLAYER_REQUEST_IDLE");
        // 指向下一首歌
        if (lcmusic_app_data->file_iterator) {
            file_iterator_next(lcmusic_app_data->file_iterator);
            int index = file_iterator_get_index(lcmusic_app_data->file_iterator);
            ESP_LOGI(TAG, "playing index '%d'", index);
            lcmusic_play_index(lcmusic_app_data, index);
        }
        break;
    }
    case AUDIO_PLAYER_CALLBACK_EVENT_PLAYING: // 正在播放音乐
        ESP_LOGI(TAG, "AUDIO_PLAYER_REQUEST_PLAY");
        sys_set_pa_level(1); // 打开音频功放
        break;
    case AUDIO_PLAYER_CALLBACK_EVENT_PAUSE: // 正在暂停音乐
        ESP_LOGI(TAG, "AUDIO_PLAYER_REQUEST_PAUSE");
        sys_set_pa_level(0); // 关闭音频功放
        break;
    default:
        break;
    }
}

void lcmusic_play_index(lcmusic_app_data_t* lcmusic_app_data, int index)
{
    ESP_LOGI(TAG, "play_index(%d)", index);

    char filename[128];
    int retval = file_iterator_get_full_path_from_index(lcmusic_app_data->file_iterator, index, filename, sizeof(filename));
    if (retval == 0) {
        ESP_LOGE(TAG, "unable to retrieve filename");
        return ;
    }

    FILE *fp = fopen(filename, "rb");
    if (fp) {
        ESP_LOGI(TAG, "Playing '%s'", filename);
        audio_player_play(fp);
        file_iterator_set_index(lcmusic_app_data->file_iterator, index);
        lcmusic_app_data->track_no = index;
    } else {
        ESP_LOGE(TAG, "unable to open index %d, filename '%s'", index, filename);
    }
}

void lcmusic_stop_play(lcmusic_app_data_t* lcmusic_app_data)
{
    printf("lcmusic_stop_play\n");
    audio_player_pause();
}

void lcmusic_start_play(lcmusic_app_data_t* lcmusic_app_data)
{
    printf("lcmusic_start_play\n");
    audio_player_resume();
}

void lcmusic_play_next(lcmusic_app_data_t* lcmusic_app_data)
{
    int index = 1;

    file_iterator_next(lcmusic_app_data->file_iterator);
    index = file_iterator_get_index(lcmusic_app_data->file_iterator);
    lcmusic_play_index(lcmusic_app_data, index);

    ESP_LOGI(TAG, "playing index '%d'", index);
}

void lcmusic_play_prev(lcmusic_app_data_t* lcmusic_app_data)
{
    int index = 1;

    file_iterator_prev(lcmusic_app_data->file_iterator);
    index = file_iterator_get_index(lcmusic_app_data->file_iterator);
    lcmusic_play_index(lcmusic_app_data, index);

    ESP_LOGI(TAG, "playing index '%d'", index);
}

void lcmusic_media_init(lcmusic_app_data_t* lcmusic_app_data)
{
    audio_player_callback_register(lcmusic_play_event_callback, lcmusic_app_data);
    lcmusic_play_index(lcmusic_app_data, lcmusic_app_data->track_no);
}

void lcmusic_media_deinit(lcmusic_app_data_t* lcmusic_app_data)
{
    audio_manager_close();
}
